/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	ARepairServiceImpl.java
 * Created：		2016年9月14日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.repair;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.entity.UCN;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AAssignInfo;
import com.thor.palmmall.rest.common.ASignInInfo;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SAssignInfoConverter;
import com.thor.palmmall.rest.common.SSignInInfoConverter;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.repair.converter.AMaterialConverter;
import com.thor.palmmall.rest.repair.converter.ARepairBillConverter;
import com.thor.palmmall.rest.repair.converter.ARepairConfigConverter;
import com.thor.palmmall.rest.repair.converter.SMaintainBillConverter;
import com.thor.palmmall.rest.repair.converter.SMaintainBillQueryParamConverter;
import com.thor.palmmall.rest.repair.converter.SMaterialQueryParamConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.basic.SMaterial;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.maintain.SDefaultConfig;
import com.thor.palmmall.spi.maintain.SMaintainBill;
import com.thor.palmmall.spi.maintain.SMaintainBillService;

/**
 * @author wolv
 *
 */
public class ARepairServiceImpl implements ARepairService {

  @Override
  public ARepairConfig getConfig(String storeUuid) throws ThorServiceException {
    Assert.assertArgumentNotNull(storeUuid, "storeUuid");

    SResponse<SDefaultConfig> resp = sMaintainBillService.getDefaultConfig(storeUuid);
    if (resp.isSuccess()) {
      return ARepairConfigConverter.getInstance().convert(resp.getBody());
    } else
      throw new ThorServiceException("无法找到指定的维修单配置：" + resp.getMessage());
  }

  @Override
  public String create(ARepairBill bill, BeanOperateInfo operateInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sMaintainBillService.create(
        SMaintainBillConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建维修单：" + resp.getMessage());
  }

  @Override
  public ARepairBill get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    Assert.assertArgumentNotNull(id, "id");
    Assert.assertArgumentNotNull(param, "param");

    SResponse<SMaintainBill> resp = sMaintainBillService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return ARepairBillConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的维修单：" + resp.getMessage());

  }

  @Override
  public void save(ARepairBill bill, BeanOperateInfo operateInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<Void> resp = sMaintainBillService.save(
        SMaintainBillConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return;
    else
      throw new ThorServiceException("无法保存维修单：" + resp.getMessage());
  }

  @Override
  public QueryResult<ARepairBill> query(ARepairBillQueryParam param) throws ThorServiceException {
    Assert.assertArgumentNotNull(param, "param");

    SResponse<SQueryResult<SMaintainBill>> resp = sMaintainBillService
        .query(SMaintainBillQueryParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), ARepairBillConverter.getInstance());
    else
      throw new ThorServiceException("无法查询维修单：" + resp.getMessage());
  }

  @Override
  public void signIn(ASignInInfo signInInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(signInInfo, "signInInfo");

    SResponse<Void> resp = sMaintainBillService
        .signIn(SSignInInfoConverter.getInstance().convert(signInInfo));
    if (resp.isSuccess())
      return;
    else
      throw new ThorServiceException("抢单失败：" + resp.getMessage());
  }

  @Override
  public void assign(AAssignInfo assignInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(assignInfo, "assignInfo");

    SResponse<Void> resp = sMaintainBillService
        .assign(SAssignInfoConverter.getInstance().convert(assignInfo));
    if (resp.isSuccess())
      return;
    else
      throw new ThorServiceException("指派维修单失败：" + resp.getMessage());
  }

  @Override
  public QueryResult<AMaterial> queryMaterial(AMaterialQueryParam param)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(param, "param");

    SResponse<SQueryResult<SMaterial>> resp = sBasicService
        .queryMaterial(SMaterialQueryParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), AMaterialConverter.getInstance());
    else
      throw new ThorServiceException("无法查询材料：" + resp.getMessage());
  }

  @Override
  public List<UCN> serviceMans() throws ThorServiceException {
    SResponse<List<UCN>> resp = sBasicService.serviceMans();
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("无法查询物业维修人员：" + resp.getMessage());
    }
  }

  @Override
  public String[] queryOutgoingState() throws ThorServiceException {
    SResponse<String[]> resp = sMaintainBillService.queryOutgoingState();
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("查询流程状态失败：" + resp.getMessage());
    }
  }

  @Autowired
  private SMaintainBillService sMaintainBillService;
  @Autowired
  private SBasicService sBasicService;

}
